package com.zzw.bridge.business.api;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.zzw.bridge.business.service.ReceiveDataService;
import com.zzw.bridge.business.vo.ZzwPierProjectVo;
import com.zzw.bridge.common.exception.ErrorCode;
import com.zzw.bridge.common.exception.ServerException;
import com.zzw.bridge.common.utils.FileUtils;
import com.zzw.bridge.common.utils.GsonUtil;
import com.zzw.bridge.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 接收外部数据接口
 *
 * @author XiaoJie
 * @date 2025/5/16 11:26
 */
@Tag(name = "接收外部数据控制器")
@RestController
@RequestMapping("business/remote")
@Slf4j
public class ReceiveExternalDataController {
    @Resource
    private ReceiveDataService receiveDataService;

    @PostMapping("/receiveData")
    @Operation(summary = "传输json数据")
    public Result<Boolean> receiveData (@RequestBody JSONObject jsonData) {
        String data = GsonUtil.toJsonStr(jsonData);
        log.info("接受到外部数据格式；{}",data);
        return Result.ok(receiveDataService.receiveData(data));
    }

    @PostMapping("/getProjectList")
    @Operation(summary = "获取账户下的项目列表")
    public Result<List<ZzwPierProjectVo>> getProjectList (HttpServletRequest request) {
        log.info("外部系统获取账户下面的项目列表");
        return Result.ok(receiveDataService.getProjectList(request));
    }

    @PostMapping("/receiveFile")
    @Operation(summary = "传输文件数据")
    public Result<HashMap<String,String>> uploadFile(@RequestParam("file") MultipartFile file,
                                                     @RequestParam(value = "reportId") String reportId ) {
        log.info("接受到外部文件：{}",file.getOriginalFilename());
        try {
            boolean isZipFile = FileUtils.isZipFile(file);
            if (!isZipFile) {
                return Result.error(ErrorCode.FILE_HANDLE_ERROR.getCode(),"请上传zip文件，其他文件暂不支持！！！");
            }
            // 异步处理上传文件
            receiveDataService.storeAndExtractZipFile(file, reportId);
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("success","true");
            hashMap.put("message","文件上传请求已接收，正在处理中");
            hashMap.put("uploadId",reportId);
            return Result.ok(hashMap);
        } catch (Exception e) {
            throw new ServerException("文件上传失败: " + e.getMessage());
        }
    }

    @PostMapping("/receiveProjectData")
    @Operation(summary = "同步项目数据")
    public Result<String> receiveProjectData (@RequestBody JSONObject jsonData) {
        JSONArray data = jsonData.getJSONArray("jsonData");
        if (data == null) {
            return Result.error("数据不可为空！！");
        }else {
            String data1 = GsonUtil.toJsonStr(data);
            log.info("接受到外部数据格式；{}",data1);
            return receiveDataService.receiveProjectData(data1);
        }

    }
}
